package com.modbus.tool.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modbus.tool.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.modbus.tool.service.IDataPointService;
import com.modbus.tool.entity.po.DataPoint;

import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: Leno
 * @Date: 2025-08-07
 * @Description: dataPoint接口
 */
@RequiredArgsConstructor
@Slf4j
@Tag(name = "dataPoint接口")
@RestController
@RequestMapping("/point")
public class DataPointController {

    private final IDataPointService dataPointService;

    @Operation(summary = "查询所有DataPoint")
    @GetMapping
    public Result<List<DataPoint>> findAll() {
        return Result.success(dataPointService.list());
    }

    @Operation(summary = "根据id查询DataPoint")
    @GetMapping("/{id}")
    public Result<DataPoint> findOne(@PathVariable Long id) {
        return Result.success(dataPointService.getById(id));
    }

    @Operation(summary = "根据分页查询DataPoint")
    @GetMapping("/page")
    public Result<Page<DataPoint>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                            DataPoint dataPoint) {
        return dataPointService.findPage(pageNum, pageSize, dataPoint);
    }

    @Operation(summary = "添加或修改DataPoint")
    @PostMapping
    public Result<Boolean> save(@RequestBody DataPoint dataPoint) {
        return Result.success(dataPointService.saveOrUpdate(dataPoint));
    }

    @Operation(summary = "根据id批量删除DataPoint")
    @DeleteMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Long> ids) {
        return dataPointService.deleteBatch(ids);
    }

    @Operation(summary = "根据deviceId查询DataPoint")
    @GetMapping("/byDeviceId/{deviceId}")
    public Result<List<DataPoint>> findPointByDeviceId(@PathVariable Long deviceId) {
        List<DataPoint> list = dataPointService.lambdaQuery().eq(DataPoint::getDeviceId, deviceId).list();
        return Result.success(list);
    }

    @Operation(summary = "批量保存点位信息")
    @PostMapping("/batch/save")
    public Result<Boolean> saveBatch(@RequestBody List<DataPoint> dataPoints) {
        return Result.success(dataPointService.addBatch(dataPoints));
    }

}
